package com.niuma.servlet;

import com.niuma.pojo.User;
import com.niuma.service.user.UserService;
import com.niuma.service.user.UserServiceImpl;
import com.niuma.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 一颗蛋50斤
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        // 和数据库中的密码进行对比，调用业务层
        UserService userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);

        if (user != null && userCode.equals(user.getUserCode()) && userPassword.equals(user.getUserPassword())) {
            req.getSession().setAttribute(Constants.USER_SESSION, user);
            resp.sendRedirect("sys/success.jsp");
        } else {
            req.setAttribute(Constants.MASSAGE, "用户名或密码不正确");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
